package com.geekgo.gges.web;

import com.alibaba.fastjson.JSON;
import com.geekgo.gges.model.Article;
import com.geekgo.gges.service.ArticleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/5/1.
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    private final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Object save(){
        Article article = new Article();
        article.setId(1L);
        article.setTitle("儿子路遇武警父亲 抱住就不愿意撒手了");
        article.setContent("两岁儿子路遇武警执勤父亲“求抱抱”，紧抱裤腿拽手不放开\n" +
                "新民晚报讯（通讯员 陆振鑫 黄梁健 记者 江跃中）“儿子双手紧紧抱着我，我却不得不哄开他，因为我还有执勤任务。”昨天下午，这一幕父子难别舍的温情画面，戳中了不少路人的泪点。原来，五一节假期间，担任巡逻任务的武警上海总队执勤五支队三中队副中队长周帆和战友们，步巡路过宝山一商业区，恰巧遇上了正在逛街的爱人和儿子。刚满2岁的孩子见着爸爸后，急忙跑到跟前，“求抱抱”不成后，紧紧抱着爸爸的裤腿，哭着拽住爸爸的手，不肯放开……");
        article.setCreateTime(new Date());
        article.setShowCount(1);
        Long aLong = articleService.saveArticle(article);
        return aLong;
    }
    @GetMapping
    public Object query(){
        List<Article> articles = articleService.searchAll();
        logger.info("查询结果：{}", JSON.toJSONString(articles));
        return articles;
    }
    @GetMapping("/word")
    public Object queryByWord(@RequestParam String word){
        List<Article> articles = articleService.queryByString(word);
        logger.info("查询结果：{}", JSON.toJSONString(articles));
        return articles;
    }
    @GetMapping("/match")
    public Object queryBymatch(@RequestParam String word){
        List<Article> articles = articleService.queryByMatch(word);
        logger.info("查询结果：{}", JSON.toJSONString(articles));
        return articles;
    }
}
